import { createOrder, getCalendartickt, getCanBuyDays, getUserInfo, getUsereExtInfo, ticketVerificationCheck } from './src/thirdServices/palaceApi';
import { signature } from './src/utils/signature';

const main = async () => {
  const params = {
    merchantId: 2655,
    merchantInfoId: 2655
  };

  const token = 'eyJhbGciOiJIUzUxMiJ9.eyJ1IjoiNTMwNTAxMTc5ODQ3NzkwNTkyIiwidCI6IjAiLCJleHAiOjE3MDkwMzM5ODJ9.mV51IIwL7Ot3ZRTuj0mjy13WE_MwpOsY-HdJej1biya1LdN9AvHh9K5uEo27CIgsQ5jAMA7rnLi9-8_BZdafiA';
  // await checkTicket(token);
  await createTicketOrder(token);
  return;
  const userRes = await getUserInfo(token, params);
  if (userRes.data.code === 200) {
    console.log('getUser');
    const userInfo = userRes.data.data;
    
    // ?id=530501179818430464&cipherText=0&merchantId=2655&merchantInfoId=2655
    const pp = {
      id: userInfo.id,
      cipherText: 0,
      merchantId: 2655,
      merchantInfoId: 2655
    }
    const extInfo = await getUsereExtInfo(token, pp);
    if (extInfo.data.status === 200) {
      // extInfo.data.data.openId
    }
    // console.log(extInfo.data);
    // phoneShow
    // id
    // 一天调用一次就行
    const dayRes = await getCanBuyDays(token);
    if (dayRes.data.code === 200) {
      // data: '{"days":6,"time":"2024-02-27 11:07:43","timestamp":1709003263093}',
    }
    const params = {
      parkId: 11324,
      year: '2024',
      month: '02',
      merchantId: 2655,
      merchantInfoId: 2655,
    };
    console.log('startChecktick');
    const calTicktRes = await getCalendartickt(token, params);
    if (calTicktRes.data.status === 200) {
      // console.log(calTicktRes.data.data);
      // {
      //   stockNum: 0,
      //   occDate: '2024-02-27',
      //   formatDate: '02月27日',
      //   saleStatus: 'T',
      //   parkFsyyDetailDTOS: [Array]
      // }
      await checkTicket(token);
    }
  } else {
    console.log(userRes.data);
  }
};

const checkTicket = async (token) => {
  // const openId = 'oOya25CysrTrkEktia4WxypLHn9Y';
  const saveInfo = {
    chooseRuleProcessors: '',
    merchantId: '2655',
    merchantInfoId: '2655',
    orderType: 'park',
    startDate: '2024-02-28',
    ticketVerificationDTOS: [
      {
        certAuthDTOS: [
          {
            cardType: 0,
            certNo: '230183199003254410',
            name: '郑昌军'
          }
        ],
        modelCodesDTOS: [
          {
            modelCode: 'MP2022070117025856157', 
            parentModelCode: 'MP2022070117025856157'
          }
        ]
      }
    ],
  };
  const res = await ticketVerificationCheck(token, saveInfo);;
  if (res.data.code === 200 && res.data.data.checkStatus) {
    console.log(res.data.data.passCertAuthList);
  }
}

const createTicketOrder = async (token) => {
  const params = signature(token, '' + Date.now());
  // const params = 'sign=26cec06b85112965ce5c0e7a7a673ca1&timestamp=1709027118536';
  const data = { 
    buyer: {
      id: '530501179818430464',
      openId: 'oOya25CysrTrkEktia4WxypLHn9Y',
      mobile: '18519203764',
      credentialNo: '230183199003254410',
      credentialType: '0',
      nickName: '郑昌军'
    },
    couponCode: '',
    startDate: '2024-02-29',
    endDate: '2024-02-29', 
    addTickets: [], 
    saveOrders: [
      {
        ticketName: '门票全价票',
        price: 40,
        amount: 1,
        modelCode: 'MP2022070117025856157',
        itemId: '75821',
        wayType: '6',
        fsName: '下午',
        orderCertAuthList: [
          {
            realName: "郑昌军",
            certType: 0,
            certNo: '230183199003254410'
          }
        ],
        needConfirm: 'F' ,
        spiltStartTime: '11:00',
        spiltEndTime: '17:00'
      }
    ],
    wayType: '6',
    orderType: 'park',
    merchantInfoId: '2655'
  };
  const res = await createOrder(token, params, data);
  console.log(res);
}


main();